package com.zhkj.shixi.tvfilemanager;

import android.util.Log;

import java.io.File;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.List;

import static com.zhkj.shixi.tvfilemanager.StaticClasses.fileManager;

/**
 * Created by linjing on 19-8-13.
 */

public class SortFiles {
    public static final int SORT_BY_NAME = 0;
    public static final int SORT_BY_DATE = 1;
    public static final int SORT_BY_SIZE = 3;

    public static List<File> sortFiles(String url, int type){
        if (type == SORT_BY_NAME) {
            return orderByName(url);
        }else if (type == SORT_BY_DATE) {
            return orderByDate(url);
        }else {
            return orderByLength(url);
        }
    }

    /**
     * 按名称排序
     * @param filePath
     */
    public static List<File> orderByName(String filePath) {
        List<File> fileList = FileManager.getTypeFiles(filePath, fileManager.getType());
        Collections.sort(fileList, new Comparator<File>() {
            @Override
            public int compare(File o1, File o2) {
                if (o1.isDirectory() && o2.isFile())
                    return -1;
                if (o1.isFile() && o2.isDirectory())
                    return 1;
                return o1.getName().compareTo(o2.getName());
            }
        });
        return fileList;
    }


    /**
     * 按文件修改日期排序
     * @param filePath
     */
    public static List<File> orderByDate(String filePath) {
        List<File> fileList = FileManager.getTypeFiles(filePath, fileManager.getType());
        Collections.sort(fileList, new Comparator<File>() {
            @Override
            public int compare(File f1, File f2) {
                long diff = f1.lastModified() - f2.lastModified();
                if (diff > 0)
                    return -1;
                else if (diff == 0)
                    return 0;
                else
                    return 1;//如果 if 中修改为 返回-1 同时此处修改为返回 1  排序就会是递减
            }


            public boolean equals(Object obj) {
                return true;
            }

        });


        return fileList;
    }

    /**
     * 按文件大小排序
     * @param filePath
     */
    public static List<File> orderByLength(String filePath) {

        List fileList = FileManager.getTypeFiles(filePath, fileManager.getType());
        Collections.sort(fileList, new Comparator<File>() {
            @Override
            public int compare(File f1, File f2) {
                long diff = f1.length() - f2.length();
                if (diff > 0)
                    return -1;
                else if (diff == 0)
                    return 0;
                else
                    return 1;//如果 if 中修改为 返回-1 同时此处修改为返回 1  排序就会是递减
            }

            public boolean equals(Object obj) {
                return true;
            }
        });
        return fileList;
    }


}
